﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using OfficeConvert;
using SocketService;
using System.Net.Sockets;

namespace WinFormOTF
{
    public partial class MainForm : Form
    {
        string pdfpath = "";
        SocketListener sl;
        public MainForm()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
        }

        /// <summary>
        /// 选择文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnfile_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog()==DialogResult.OK)
            {
                txtfilepath.Text = ofd.FileName;
                txtoutfilepath.Text = Path.GetDirectoryName(txtfilepath.Text)+@"\";
            }
        }

        /// <summary>
        /// office转换为pdf
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnok_Click(object sender, EventArgs e)
        {
            pdfpath = txtoutfilepath.Text + Guid.NewGuid() + ".pdf";
            OfficeToPdf.DoConvert(txtfilepath.Text, pdfpath);
            labpdf.Text = Path.GetFileName(pdfpath);
            MessageBox.Show("转换成功！");
        }

        /// <summary>
        /// 选择输出目录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnoutfile_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                txtoutfilepath.Text = fbd.SelectedPath+@"\";
            }
        }

        /// <summary>
        /// pdf转flash
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnflash_Click(object sender, EventArgs e)
        {
            PdfToFlash.DoConvert(txtfilepath.Text, Path.GetDirectoryName(txtfilepath.Text) + @"\" + Guid.NewGuid() + ".swf");
            MessageBox.Show("转换成功！");
        }

        /// <summary>
        /// office转flash
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnotf_Click(object sender, EventArgs e)
        {
            var filename=Path.GetFileName(txtfilepath.Text);
            filename=filename.Substring(0,filename.IndexOf('.'));
            
            //不建议使用原文件名，防止在多线程操作中出现冲突
            var outfile=txtoutfilepath.Text+@"\"+filename+".swf";
            var result = OfficeToFlash.DoConvert(txtfilepath.Text, outfile);
            if (result)
            {
                MessageBox.Show("转换成功！");
            }
            else
            {
                MessageBox.Show("转换失败！");
            }
        }

       /// <summary>
       /// 启动监听按钮
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
        private void btnstateserver_Click(object sender, EventArgs e)
        {
            sl = new SocketListener("127.0.0.1", 23456);
            sl.Commendeven += DoSomthing;
            ShowLog("正在监听本机23456端口.....");
            btnstateserver.Enabled = false;
        }

        /// <summary>
        /// 收到命令触发事件
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="socket"></param>
        private void DoSomthing(string cmd,Socket socket)
        {
            ShowLog("接收到一组客户端命令！");
            ShowLog(cmd);
            //officeconvert|e://file/abc.doc|d://filefolder/flash/|1
            string[] comstr = cmd.Split(new Char[] { '|' }, 4);
            if (comstr[0] == "officeconvert")
            {
                string filepath=comstr[1];
                string filefolder = "";
                string outfile;
                filefolder = comstr[2];
                if (comstr.Length >= 4 && comstr[3] == "1")//转pdf
                {
                    ShowLog("正在将文件转换为pdf");
                    outfile =filefolder+ Guid.NewGuid() + ".pdf";
                    var res = OfficeToPdf.DoConvert(filepath, outfile);
                    if (res)
                    {
                        //处理后续
                        socket.Send("result|"+outfile);
                    }
                }
                else
                {
                    rtbresult.AppendText("正在将文件转换为flash");
                    rtbresult.AppendText(Environment.NewLine);
                    outfile = filefolder + Guid.NewGuid() + ".swf";
                    var res = OfficeToFlash.DoConvert(filepath, outfile);
                    if (res)
                    {
                        //处理后续
                        socket.Send("result|" + outfile);
                    }
                }
                ShowLog("文件已生成文件：" + outfile);
            }
            else if (comstr[0] == "msg")
            {
                ShowLog(string.Format("收到命令msg:{0}", comstr[1]));
            }

            socket.Send("msg|我已经受到你的命令!");
            if (socket!=null)
            {
                socket.Close();
            }

        }

        private void ShowLog(string msg)
        {
            rtbresult.AppendText(msg);
            rtbresult.AppendText(Environment.NewLine);
            if (rtbresult.TextLength>1000)
            {
                rtbresult.Clear();
            }
        }

        private void DoConvert(string filePath)
        {
            
        }

        /// <summary>
        /// 停止监听
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnstopserver_Click(object sender, EventArgs e)
        {
            sl.Close();
            ShowLog("已经停止监听");
            btnstateserver.Enabled = true;
        }
    }
}
